package com.example.goodpet.my.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.goodpet.R;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.User;
import com.example.goodpet.bean.UserTieziInfo;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.my.adapter.MyFensiAdapter;
import com.example.goodpet.my.adapter.MyTieziCollectAdapter;
import com.example.goodpet.my.fragment.MyTieziInfoFragment;
import com.example.goodpet.service.CommunityService;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.ActivityManager;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MyLikesActivity extends FragmentActivity {
    private  final String TAG = this.getClass().getSimpleName();
    private TextView cmTitleText;
    private ImageView cmTitleLeft;
    private TextView cmTitleRight;

    private RecyclerView recyclerView;

    private List<UserTieziInfo> data;

    private Retrofit retrofit;
    private CommunityService communityService;
    private long userId;

    LoadMoreWrapper loadMoreWrapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_fensi);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addActivity(this);

        cmTitleLeft=findViewById(R.id.cmTitleLeft);
        cmTitleText=findViewById(R.id.cmTitleText);
        cmTitleRight=findViewById(R.id.cmTitleRight);
        recyclerView=findViewById(R.id.recyclerView);

        userId=getIntent().getLongExtra("userId",-1);

        initListener();
        initTitle();
    }
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        retrofit= RetrofitSingleton.getRetrofitNetConfig();
        communityService=retrofit.create(CommunityService.class);
        communityService.getUserLikes(userId,1).enqueue(new Callback<ResponseBody<List<UserTieziInfo>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<UserTieziInfo>>> call, Response<ResponseBody<List<UserTieziInfo>>> response) {
                ResponseBody<List<UserTieziInfo>> body = response.body();
                if (body!=null){
                    if (body.getCode()==200){
                        data=body.getData();
                        //Log.d(TAG, "onResponse: "+);
                        MyTieziCollectAdapter myTieziCollectAdapter = new MyTieziCollectAdapter(data,MyLikesActivity.this,MyLikesActivity.this);
                        loadMoreWrapper = new LoadMoreWrapper(myTieziCollectAdapter);
                        recyclerView.setAdapter(loadMoreWrapper);
                        recyclerView.setLayoutManager(new GridLayoutManager(MyLikesActivity.this,1));
                        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
                            @Override
                            public void onLoadMore() {
                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                                communityService.getUserLikes(MyApplication.user.getId(), ++loadMoreWrapper.page).enqueue(new Callback<ResponseBody<List<UserTieziInfo>>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<List<UserTieziInfo>>> call, Response<ResponseBody<List<UserTieziInfo>>> response) {
                                        ResponseBody<List<UserTieziInfo>> body1 = response.body();
                                        //Log.d(TAG, "onResponse: "+ body);
                                        if (body1.getCode()==200){
                                            List<UserTieziInfo> data1 = body1.getData();
                                            data.addAll(data1);
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                                        }else if (body1.getCode()==404){
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                        }

                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<List<UserTieziInfo>>> call, Throwable t) {
                                        t.printStackTrace();
                                        UIUtils.toast("连接服务器失败",false);
                                    }
                                });
                            }
                        });

                    }else if (body.getCode()==404){
                        UIUtils.toast("数据为空",false);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<UserTieziInfo>>> call, Throwable t) {
                t.printStackTrace();
                UIUtils.toast("连接服务器失败",false);
            }
        });
    }

    private void initTitle() {
        cmTitleText.setText("我点赞的");
        cmTitleRight.setVisibility(View.INVISIBLE);
    }

    private void initListener() {
        cmTitleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManager.getInstance().removeCurrent();
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK)
            if (requestCode==1){
                com.example.goodpet.community.bean.UserTieziInfo info = data.getParcelableExtra("info");
                int position = data.getIntExtra("position", -1);
                if (position!=-1 && info!=null) refresh(position,info);
            }
    }
    public void refresh(int position, com.example.goodpet.community.bean.UserTieziInfo info){
        UserTieziInfo userTieziInfo = data.get(position);
        long userId = userTieziInfo.getUserId();
        for (UserTieziInfo tieziInfo : data) {
            if(tieziInfo.getUserId()==userId) tieziInfo.setFoucs(info.isFoucs());
        }
        userTieziInfo.setLike(info.isLike());
        userTieziInfo.setLikeNum(info.getLikeNum());
        userTieziInfo.setCollect(info.isCollect());
        userTieziInfo.setCollectNum(info.getCollectNum());
        userTieziInfo.setMessageNum(info.getMessageNum());
        userTieziInfo.setFoucs(info.isFoucs());
        loadMoreWrapper.notifyDataSetChanged();
    }
}